package cn.websocket.common;

import cn.dev33.satoken.exception.NotLoginException;
import cn.websocket.vo.Result;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import static cn.websocket.constant.StatusConstant.TOKEN_INVALID;

/**
 * <h3>WebSocket</h3>
 * <p>全局异常处理类</p>
 *
 * @author gdw
 * @since 2025-08-18 15:20:35
 */
@RestControllerAdvice
public class GlobalExceptionHandler {
    @ExceptionHandler(MethodArgumentNotValidException.class) //拦截Valid参数校验异常
    public Result handleValidationError(MethodArgumentNotValidException ex) {
        String errorMsg = ex.getBindingResult()
                .getFieldError()
                .getDefaultMessage();
        return Result.fail(errorMsg);
    }
    @ExceptionHandler(NotLoginException.class)//拦截token无效异常
    public Result handleNotLoginException(NotLoginException ex) {
        String message = ex.getMessage();
        return Result.fail(TOKEN_INVALID,message);
    }
}
